import {createRouter, createWebHashHistory} from "vue-router";
import Home from "../views/Home.vue";

const routes = [
    {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import ("../views/Login.vue")
    },
    {
        path: '/',
        redirect: '/dashboard'
    },
    {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/doctor",
                name: "doctor",
                meta: {
                    title: '医生信息'
                },
                component: () => import ("../views/temp/Doctor.vue")
            },
            {
                path: "/statisticLine",
                name: "statisticLine",
                meta: {
                    title: '条形图统计'
                },
                component: () => import ("../views/temp/StatisticLine.vue")
            },
            {
                path: "/statisticBar",
                name: "statisticBar",
                meta: {
                    title: '柱状图统计'
                },
                component: () => import ("../views/temp/StatisticBar.vue")
            },
            {
                path: "/statisticPie",
                name: "statisticPie",
                meta: {
                    title: '饼状图统计'
                },
                component: () => import ("../views/temp/StatisticPie.vue")
            },
            {
                path: "/doctorForm",
                name: "doctorForm",
                meta: {
                    title: '新增医生'
                },
                component: () => import ("../views/temp/DoctorForm.vue")
            },
            {
                path: "/medicine",
                name: "medicine",
                meta: {
                    title: '药品信息'
                },
                component: () => import ("../views/temp/Medicine.vue")
            },
            {
                path: "/medicineForm",
                name: "medicineForm",
                meta: {
                    title: '新增药品'
                },
                component: () => import ("../views/temp/MedicineForm.vue")
            },
            {
                path: "/supplier",
                name: "supplier",
                meta: {
                    title: '供应商信息'
                },
                component: () => import ("../views/temp/Supplier.vue")
            },
            {
                path: "/supplierForm",
                name: "supplierForm",
                meta: {
                    title: '新增供应商'
                },
                component: () => import ("../views/temp/SupplierForm.vue")
            },
            {
                path: "/bill",
                name: "bill",
                meta: {
                    title: '采购信息'
                },
                component: () => import ("../views/temp/Bill.vue")
            },
            {
                path: "/billForm",
                name: "billForm",
                meta: {
                    title: '采购登记'
                },
                component: () => import ("../views/temp/BillForm.vue")
            },
            {
                path: "/patientBill",
                name: "patientBill",
                meta: {
                    title: '销售信息'
                },
                component: () => import ("../views/temp/PatientBill.vue")
            },
            {
                path: "/patientBillForm",
                name: "patientBillForm",
                meta: {
                    title: '销售登记'
                },
                component: () => import ("../views/temp/PatientBillForm.vue")
            },
            {
                path: "/adminForm",
                name: "adminForm",
                meta: {
                    title: '新增账号'
                },
                component: () => import ("../views/temp/adminForm.vue")
            },
            {
                path: "/adminUpdateForm",
                name: "adminUpdateForm",
                meta: {
                    title: '修改密码'
                },
                component: () => import ("../views/temp/adminUpdateForm.vue")
            },
            {
                path: "/admin",
                name: "admin",
                meta: {
                    title: '账号信息'
                },
                component: () => import ("../views/temp/admin.vue")
            },
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: '系统首页'
                },
                component: () => import ( /* webpackChunkName: "dashboard" */ "../views/Dashboard.vue")
            },{
                path: '/404',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */ '../views/404.vue')
            }, {
                path: '/403',
                name: '403',
                meta: {
                    title: '没有权限'
                },
                component: () => import (/* webpackChunkName: "403" */ '../views/403.vue')
            }
        ]
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const ms_username = localStorage.getItem('ms_username');
    const role = localStorage.getItem('role');
    const doctorPath = ['/login', '/dashboard', '/statistic','/',
        '/medicine', '/patientBill', '/patientBillForm',
        '/adminUpdateForm', '/403', '/404'];
    const purchaseManPath = ['/login', '/dashboard','/statistic', '/',
        '/medicine', '/medicineForm','/bill', '/billForm',
        '/supplier', '/supplierForm', '/adminUpdateForm', '/403', '/404'];
    if (!ms_username && to.path !== '/login') {
        next('/login');
    } else {
        console.log(to.path);
        if ((role === '1' || role === 1) &&
            !doctorPath.find(item => { return item === to.path })) {
            next('/403');
        } else if ((role === '2' || role === 2) &&
            !purchaseManPath.find(item => { return item === to.path })) {
            next('/403');
        } else {
            next();
        }
    }
});

export default router;